﻿﻿
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>

<head>
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Expires" content="0"/>
    <link rel="stylesheet" href="${ctx}/common/common.css"/>
    <title>Title</title>
    <link rel="stylesheet"
          href="${ctx}/common/plugins/layuiadmin/layui/css/layui.css?t=<%=System.currentTimeMillis()%>" media="all"/>
    <link rel="stylesheet"
          href="${ctx}/common/plugins/layuiadmin/style/admin.css?t=<%=System.currentTimeMillis()%>" media="all"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"/>
    <script src="${ctx}/common/plugins/layuiadmin/layui/layui.js?t=<%=System.currentTimeMillis()%>"></script>
    <script
            src="${ctx}/common/plugins/layuiadmin/layui-xtree/layui-xtree.js?t=<%=System.currentTimeMillis()%>"></script>
    <style type="text/css">
        .rowtop {
            padding: 10px;
        }

        .layui-layer-msg {
            position: fixed !important;
            top: 50% !important;
        }

        .file-preview {
            display: inline-block;
        }

        .file-item {
            position: relative;
            width: 100px;
            height: 120px;
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            padding: 5px;
            text-align: center;
            background: #f8f8f8;
        }

        .file-icon {
            font-size: 40px;
            height: 70px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .file-icon.img {
            color: #e74c3c;
        }

        .file-icon.excel {
            color: #27ae60;
        }

        .file-icon.pdf {
            color: #e67e22;
        }

        .file-icon.word {
            color: #3498db;
        }

        .file-icon.other {
            color: #7f8c8d;
        }

        .file-remove {
            position: absolute;
            top: -8px;
            right: -8px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: #ff5722;
            color: white;
            text-align: center;
            line-height: 20px;
            cursor: pointer;
            font-size: 12px;
            display: none;
        }

        .file-item:hover .file-remove {
            display: block;
        }

        .file-name {
            font-size: 12px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-top: 5px;
        }

        .file-preview-img {
            max-width: 100px;
            max-height: 70px;
            object-fit: contain;
        }

        /* 生产范围树形结构样式，与区域选择器保持一致 */
        .proclashow .xtree_contianer {
            padding: 10px;
        }

        .proclashow .xtree_contianer ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .proclashow .xtree_contianer li {
            padding: 5px 0;
            line-height: 24px;
        }

        .proclashow .xtree_contianer .layui-form-checkbox {
            margin-right: 5px;
        }

        .proclashow .xtree_contianer .layui-form-checkbox span {
            padding-left: 20px;
        }

        /* 确保树形结构内的复选框样式统一 */
        .proclashow .layui-form-checkbox {
            line-height: 24px;
        }

        .layui-unselect.layui-form-select {
            width: 100%;
        }
    </style>
</head>

<body>
<form class="layui-form" action="${ctx}/enterprise/saveEnterprise" method="post"
      enctype="multipart/form-data">
    <input type="hidden" name="id" value="${tenterprise.id}"/>
    <input type="hidden" name="deleteFlag" value="0"/>
    <input type="hidden" name="licenceId" value="${licence1.id }"/>
    <input type="hidden" name="licenceId2" value="${licence2.id }"/>
    <input type="hidden" name="changeMaterials" id="changeMaterials" value="${tenterprise.changeMaterials}"/>
    <div class="layui-row rowtop" style="margin-top: 20px">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                企业名称
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="name" value="${tenterprise.name}" lay-verify="required"
                       placeholder="请输入企业名称"
                       autocomplete="off" class="layui-input" disabled/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">企业简称</label>
            <div class="layui-input-block">
                <input type="text" name="abbreviation" value="${tenterprise.abbreviation}" placeholder="请输入企业简称"
                       autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                区域
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <div class="area-select-container" style="position: relative; width: 100%">
                    <div id="areaDiv" style="
                  display: block;
                  width: 100%;
                  height: 38px;
                  background: #fff;
                  line-height: 38px;
                  border: 1px solid #e6e6e6;
                  border-radius: 2px;
                  cursor: pointer;
                  position: relative;
                ">
                        <c:choose>
                            <c:when test="${empty areaname}">
                                <span style="color: #999; margin-left: 10px">请选择区域</span>
                            </c:when>
                            <c:otherwise>
                                <span style="color: #333; margin-left: 10px">${areaname}</span>
                            </c:otherwise>
                        </c:choose>
                    </div>
                    <i class="layui-icon layui-icon-down" style="
                  position: absolute;
                  right: 10px;
                  top: 50%;
                  transform: translateY(-50%);
                  color: #999;
                  transition: transform 0.3s ease;
                  cursor: pointer;
                  pointer-events: auto;
                  z-index: 1;
                " id="areaArrow"></i>
                    <i class="layui-icon layui-icon-close" id="areaClear" title="清空选择"
                       style="${empty areaname ? 'display: none;' : 'position: absolute; right: 30px; top: 50%; transform: translateY(-50%); color: #999; cursor: pointer; pointer-events: auto; z-index: 1; font-size: 12px;'}"></i>
                    <input type="hidden" id="t_a_id" name="t_a_id" value="${tenterprise.t_a_id }"/>
                    <div class="areaTreeshow" style="
                  width: 100%;
                  max-height: 300px;
                  position: absolute;
                  top: 100%;
                  left: 0;
                  background: white;
                  z-index: 1000;
                  border: 1px solid #e6e6e6;
                  border-top: none;
                  border-radius: 0 0 2px 2px;
                  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                  display: none;
                  overflow: hidden;
                  overflow-y: auto;
                ">
                        <ul id="areaTree"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">
                行业类别
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <div class="industry-select-container" style="position: relative; width: 100%">
                    <div id="industryDiv" style="
                  display: block;
                  width: 100%;
                  height: 38px;
                  background: #fff;
                  line-height: 38px;
                  border: 1px solid #e6e6e6;
                  border-radius: 2px;
                  cursor: pointer;
                  position: relative;
                ">
                        <c:choose>
                            <c:when test="${empty industryname}">
                                <span style="color: #999; margin-left: 10px">请选择行业类别</span>
                            </c:when>
                            <c:otherwise>
                                <span style="color: #333; margin-left: 10px">${industryname}</span>
                            </c:otherwise>
                        </c:choose>
                    </div>
                    <i class="layui-icon layui-icon-down" style="
                  position: absolute;
                  right: 10px;
                  top: 50%;
                  transform: translateY(-50%);
                  color: #999;
                  transition: transform 0.3s ease;
                  cursor: pointer;
                  pointer-events: auto;
                  z-index: 1;
                " id="industryArrow"></i>
                    <i class="layui-icon layui-icon-close" id="industryClear" title="清空选择"
                       style="${empty industryname ? 'display: none;' : 'position: absolute; right: 30px; top: 50%; transform: translateY(-50%); color: #999; cursor: pointer; pointer-events: auto; z-index: 1; font-size: 12px;'}"></i>
                    <input type="hidden" id="t_i_id" name="t_i_id" value="${tenterprise.t_i_id }"/>
                    <div class="industryshow" style="
                  width: 100%;
                  max-height: 300px;
                  position: absolute;
                  top: 100%;
                  left: 0;
                  background: white;
                  z-index: 1000;
                  border: 1px solid #e6e6e6;
                  border-top: none;
                  border-radius: 0 0 2px 2px;
                  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                  display: none;
                  overflow: hidden;
                  overflow-y: auto;
                ">
                        <ul id="industryTree"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                生产范围
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <div class="procla-select-container" style="position: relative; width: 100%">
                    <div id="proclaDiv" style="
                  display: block;
                  width: 100%;
                  height: 38px;
                  background: #fff;
                  line-height: 38px;
                  border: 1px solid #e6e6e6;
                  border-radius: 2px;
                  cursor: pointer;
                  position: relative;
                ">
                        <c:choose>
                            <c:when test="${empty proclaname}">
                                <span style="color: #999; margin-left: 10px">请选择生产范围</span>
                            </c:when>
                            <c:otherwise>
                                <span style="color: #333; margin-left: 10px">${proclaname}</span>
                            </c:otherwise>
                        </c:choose>
                    </div>
                    <i class="layui-icon layui-icon-down" style="
                  position: absolute;
                  right: 10px;
                  top: 50%;
                  transform: translateY(-50%);
                  color: #999;
                  transition: transform 0.3s ease;
                  cursor: pointer;
                  pointer-events: auto;
                  z-index: 1;
                " id="proclaArrow"></i>
                    <i class="layui-icon layui-icon-close" id="proclaClear" title="清空选择"
                       style="${empty proclaname ? 'display: none;' : 'position: absolute; right: 30px; top: 50%; transform: translateY(-50%); color: #999; cursor: pointer; pointer-events: auto; z-index: 1; font-size: 12px;'}"></i>
                    <input type="hidden" id="proclaId" name="proclaId" value="${proclaId }"/>
                    <div class="proclashow" style="
                  width: 100%;
                  max-height: 300px;
                  position: absolute;
                  top: 100%;
                  left: 0;
                  background: white;
                  z-index: 1000;
                  border: 1px solid #e6e6e6;
                  border-top: none;
                  border-radius: 0 0 2px 2px;
                  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                  display: none;
                  overflow: hidden;
                  overflow-y: auto;
                ">
                        <div id="proclaTree" class="xtree_contianer" style="padding: 10px"></div>
                        <div style="padding: 10px; border-top: 1px solid #e6e6e6; text-align: right; background: #fafafa">
                            <button type="button" id="proclaConfirm" class="layui-btn layui-btn-sm"
                                    style="margin-right: 10px">
                                确认
                            </button>
                            <button type="button" id="proclaCancel"
                                    class="layui-btn layui-btn-sm layui-btn-primary">取消
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">企业网址</label>
            <div class="layui-input-block">
                <input type="text" name="website" value="${tenterprise.website}" placeholder="请输入企业网址"
                       autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                许可证编号
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="sc" value="${tenterprise.sc}" lay-verify="required"
                       placeholder="请输入企业SC号"
                       autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">
                统一社会信用代码
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="code" value="${tenterprise.code}" lay-verify="required"
                       placeholder="请输入营业执照代码" autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">

        <div class="layui-col-md6">
            <label class="layui-form-label">
                经济性质
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <select name="type" lay-filter="aihao">
                    <c:forEach items="${entType }" var="ent">
                        <c:choose>
                            <c:when test="${tenterprise.type == ent.code}">
                                <option value="${ent.code }" selected>${ent.described }</option>
                            </c:when>
                            <c:otherwise>
                                <option value="${ent.code }">${ent.described }</option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">注册地址</label>
            <div class="layui-input-block">
                <input type="text" name="regAddress" value="${tenterprise.regAddress}" placeholder="请输入注册地址"
                       autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">生产地址</label>
            <div class="layui-input-block">
                <input type="text" name="productAddress" value="${tenterprise.productAddress}"
                       placeholder="请输入生产地址"
                       autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                法人
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="legalPerson" value="${tenterprise.legalPerson}" lay-verify="required|isName"
                       placeholder="请输入法人" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">
                联系人
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="contact" value="${tenterprise.contact}" lay-verify="required|isName"
                       placeholder="请输入联系人" autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                联系电话
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="phone" value="${tenterprise.phone}" lay-verify="phone|number"
                       placeholder="请输入联系电话" autocomplete="off" class="layui-input"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                安全管理员
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <input type="text" name="securityOfficer" value="${tenterprise.securityOfficer}" lay-verify="required"
                       placeholder="请输入安全管理员" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">
                生产状态
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <select name="productStat" lay-filter="aihao">
                    <c:forEach items="${produceType }" var="produce">
                        <c:choose>
                            <c:when test="${tenterprise.productStat == produce.code}">
                                <option value="${produce.code }" selected>${produce.described }</option>
                            </c:when>
                            <c:otherwise>
                                <option value="${produce.code }">${produce.described }</option>
                            </c:otherwise>
                        </c:choose>
                    </c:forEach>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">传真</label>
            <div class="layui-input-block">
                <input type="text" name="fax" value="${tenterprise.fax}" placeholder="请输入传真" autocomplete="off"
                       class="layui-input"/>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">邮编</label>
            <div class="layui-input-block">
                <input type="text" name="postcode" value="${tenterprise.postcode}" placeholder="请输入邮编"
                       autocomplete="off" class="layui-input" lay-verify="isPostCode"/>
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">
                审核时间
                <span style="color: red">*</span>
            </label>
            <div class="layui-input-block">
                <fmt:formatDate value="${tenterprise.approvalTime}" pattern="yyyy-MM-dd HH:mm:ss"
                                var="formattedApprovalTime"/>
                <input type="text" id="approvalTime" name="approvalTime" value="${formattedApprovalTime}"
                       readonly="readonly" lay-verify="required" placeholder="请选择审核时间" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-row rowtop">
        <div class="layui-col-md6">
            <label class="layui-form-label">企业简介</label>
            <div class="layui-input-block">
                  <textarea name="introduction" placeholder="请输入企业简介" class="layui-textarea">
                      ${tenterprise.introduction }</textarea>
            </div>
        </div>
    </div>
    <fieldset class="layui-elem-field layui-field-title" style="margin: 0px">
        <legend class="layui-add-title">生产许可证</legend>
        <div class="layui-row rowtop">
            <div class="layui-col-md6">
                <label class="layui-form-label">发放日期<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" id="grantDate" name="grantDate"
                           value="<fmt:formatDate value='${licence1.grantDate}' pattern="yyyy-MM-dd"/>"
                           readonly="readonly" lay-verify="required" placeholder="请选择发放日期" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">失效日期<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" id="invalidDate" name="invalidDate"
                           value="<fmt:formatDate value='${licence1.invalidDate}' pattern="yyyy-MM-dd"/>"
                           readonly="readonly" lay-verify="required" placeholder="请选择失效日期" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-row rowtop">
            <div class="layui-col-md5">
                <label class="layui-form-label">许可证图片<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="file" name="licenseImgFile" id="licenseImgFile" style="display: none;"
                           accept=".jpg,.jpeg,.png,.gif,.bmp,.webp">
                    <button type="button" class="layui-btn" id="licenseImgUpload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <div class="file-preview" id="licenseImgPreview">
                        <c:if test="${!empty licence1.licenceImg && fn:length(licence1.licenceImg)>0 && licence1.licenceImg != 'null'}">
                            <div class="file-item">
                                <div class="file-remove" onclick="removeLicenseImgFile()"><i
                                        class="layui-icon">&#x1006;</i></div>

                                <div class="file-icon">
                                    <c:choose>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileType == 'image'}">
                                            <img src="${ctx}/files/${licenseFileInfo.serverFileName}"
                                                 class="file-preview-img"
                                                 onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                                            <div style="display:none; color: #999; font-size: 12px;">图片加载失败
                                            </div>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileType == 'excel'}">
                                            <i class="fas fa-file-excel excel"></i>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileType == 'pdf'}">
                                            <div class="file-icon pdf">
                                                <i class="fas fa-file-pdf "></i>
                                            </div>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileType == 'word'}">
                                            <i class="fas fa-file-word word"></i>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileType == 'text'}">
                                            <i class="fas fa-file-alt text"></i>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true}">
                                            <i class="fas fa-file other"></i>
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == false}">
                                            <i class="fas fa-exclamation-triangle" style="color: #ff5722;"></i>
                                        </c:when>
                                        <c:otherwise>
                                            <c:choose>
                                                <c:when test="${fn:endsWith(licence1.licenceImg, '.jpg') || fn:endsWith(licence1.licenceImg, '.png') || fn:endsWith(licence1.licenceImg, '.gif')}">
                                                    <c:set var="fileName"
                                                           value="${fn:substringAfter(licence1.licenceImg, '/files/')}"/>
                                                    <img src="${ctx}/files/${fileName}"
                                                         class="file-preview-img"
                                                         onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                                                    <div style="display:none; color: #999; font-size: 12px;">
                                                        图片加载失败
                                                    </div>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence1.licenceImg, '.xls') || fn:endsWith(licence1.licenceImg, '.xlsx')}">
                                                    <i class="fas fa-file-excel excel"></i>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence1.licenceImg, '.pdf')}">
                                                    <div class="file-icon pdf">
                                                        <i class="fas fa-file-pdf "></i>
                                                    </div>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence1.licenceImg, '.doc') || fn:endsWith(licence1.licenceImg, '.docx')}">
                                                    <i class="fas fa-file-word word"></i>
                                                </c:when>
                                                <c:otherwise>
                                                    <i class="fas fa-file other"></i>
                                                </c:otherwise>
                                            </c:choose>
                                        </c:otherwise>
                                    </c:choose>
                                </div>
                                <div class="file-name"
                                     title="${!empty licenseFileInfo ? licenseFileInfo.fileName : '已上传文件'}">
                                    <c:choose>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileName != null}">
                                            ${licenseFileInfo.fileName}
                                        </c:when>
                                        <c:when test="${!empty licenseFileInfo && licenseFileInfo.exists == false}">
                                            <span style="color: #ff5722;">${licenseFileInfo.fileName}</span>
                                        </c:when>
                                        <c:otherwise>
                                            已上传文件
                                        </c:otherwise>
                                    </c:choose>
                                </div>
                                <c:if test="${!empty licenseFileInfo && licenseFileInfo.exists == true && licenseFileInfo.fileSize != null}">
                                    <div class="file-size" style="font-size: 10px; color: #999; margin-top: 2px;">
                                        <c:choose>
                                            <c:when test="${licenseFileInfo.fileSize > 1048576}">
                                                <fmt:formatNumber value="${licenseFileInfo.fileSize / 1048576.0}"
                                                                  maxFractionDigits="1"/>MB
                                            </c:when>
                                            <c:when test="${licenseFileInfo.fileSize > 1024}">
                                                <fmt:formatNumber value="${licenseFileInfo.fileSize / 1024.0}"
                                                                  maxFractionDigits="1"/>KB
                                            </c:when>
                                            <c:otherwise>
                                                ${licenseFileInfo.fileSize}B
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </c:if>
                            </div>
                        </c:if>
                    </div>
                </div>
            </div>
        </div>
    </fieldset>

    <fieldset class="layui-elem-field layui-field-title" style="margin: 0px">
        <legend class="layui-add-title">营业执照</legend>
        <div class="layui-row rowtop">
            <div class="layui-col-md6">
                <label class="layui-form-label">发放日期<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" id="grantDate2" name="issueDate"
                           value="<fmt:formatDate value='${licence2.grantDate}' pattern="yyyy-MM-dd"/>"
                           readonly="readonly" lay-verify="required" placeholder="请选择发放日期" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">失效日期<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" id="invalidDate2" name="expireDate"
                           value="<fmt:formatDate value='${licence2.invalidDate}' pattern="yyyy-MM-dd"/>"
                           readonly="readonly" lay-verify="required" placeholder="请选择失效日期" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-row rowtop">
            <div class="layui-col-md5">
                <label class="layui-form-label">营业执照<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="file" name="businessLicenseImgFile" id="businessLicenseImgFile" style="display: none;"
                           accept=".jpg,.jpeg,.png,.gif,.bmp,.webp">
                    <button type="button" class="layui-btn" id="businessLicenseImgUpload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <div class="file-preview" id="businessLicenseImgPreview">
                        <c:if test="${!empty licence2.licenceImg && fn:length(licence2.licenceImg)>0 && licence2.licenceImg != 'null'}">
                            <div class="file-item">
                                <div class="file-remove" onclick="removeBusinessLicenseImgFile()"><i
                                        class="layui-icon">&#x1006;</i></div>

                                <div class="file-icon">
                                    <c:choose>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileType == 'image'}">
                                            <img src="${ctx}/files/${businessLicenseFileInfo.serverFileName}"
                                                 class="file-preview-img"
                                                 onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                                            <div style="display:none; color: #999; font-size: 12px;">图片加载失败
                                            </div>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileType == 'excel'}">
                                            <i class="fas fa-file-excel excel"></i>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileType == 'pdf'}">
                                            <div class="file-icon pdf">
                                                <i class="fas fa-file-pdf "></i>
                                            </div>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileType == 'word'}">
                                            <i class="fas fa-file-word word"></i>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileType == 'text'}">
                                            <i class="fas fa-file-alt text"></i>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true}">
                                            <i class="fas fa-file other"></i>
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == false}">
                                            <i class="fas fa-exclamation-triangle" style="color: #ff5722;"></i>
                                        </c:when>
                                        <c:otherwise>
                                            <c:choose>
                                                <c:when test="${fn:endsWith(licence2.licenceImg, '.jpg') || fn:endsWith(licence2.licenceImg, '.png') || fn:endsWith(licence2.licenceImg, '.gif')}">
                                                    <c:set var="fileName"
                                                           value="${fn:substringAfter(licence2.licenceImg, '/files/')}"/>
                                                    <img src="${ctx}/files/${fileName}"
                                                         class="file-preview-img"
                                                         onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                                                    <div style="display:none; color: #999; font-size: 12px;">
                                                        图片加载失败
                                                    </div>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence2.licenceImg, '.xls') || fn:endsWith(licence2.licenceImg, '.xlsx')}">
                                                    <i class="fas fa-file-excel excel"></i>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence2.licenceImg, '.pdf')}">
                                                    <div class="file-icon pdf">
                                                        <i class="fas fa-file-pdf "></i>
                                                    </div>
                                                </c:when>
                                                <c:when test="${fn:endsWith(licence2.licenceImg, '.doc') || fn:endsWith(licence2.licenceImg, '.docx')}">
                                                    <i class="fas fa-file-word word"></i>
                                                </c:when>
                                                <c:otherwise>
                                                    <i class="fas fa-file other"></i>
                                                </c:otherwise>
                                            </c:choose>
                                        </c:otherwise>
                                    </c:choose>
                                </div>
                                <div class="file-name"
                                     title="${!empty businessLicenseFileInfo ? businessLicenseFileInfo.fileName : '已上传文件'}">
                                    <c:choose>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileName != null}">
                                            ${businessLicenseFileInfo.fileName}
                                        </c:when>
                                        <c:when test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == false}">
                                            <span style="color: #ff5722;">${businessLicenseFileInfo.fileName}</span>
                                        </c:when>
                                        <c:otherwise>
                                            已上传文件
                                        </c:otherwise>
                                    </c:choose>
                                </div>
                                <c:if test="${!empty businessLicenseFileInfo && businessLicenseFileInfo.exists == true && businessLicenseFileInfo.fileSize != null}">
                                    <div class="file-size" style="font-size: 10px; color: #999; margin-top: 2px;">
                                        <c:choose>
                                            <c:when test="${businessLicenseFileInfo.fileSize > 1048576}">
                                                <fmt:formatNumber value="${businessLicenseFileInfo.fileSize / 1048576.0}"
                                                                  maxFractionDigits="1"/>MB
                                            </c:when>
                                            <c:when test="${businessLicenseFileInfo.fileSize > 1024}">
                                                <fmt:formatNumber value="${businessLicenseFileInfo.fileSize / 1024.0}"
                                                                  maxFractionDigits="1"/>KB
                                            </c:when>
                                            <c:otherwise>
                                                ${businessLicenseFileInfo.fileSize}B
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </c:if>
                            </div>
                        </c:if>
                    </div>
                </div>
            </div>
        </div>
    </fieldset>

    <div class="layui-row rowtop">
        <div class="layui-form-item layui-col-md10">
            <label class="layui-form-label">变更材料</label>
            <div class="layui-input-block">
                <input type="file" name="file" id="fileInput" style="display: none"
                       accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.xls,.xlsx"/>
                <button type="button" class="layui-btn" id="uploadBtn">
                    <i class="layui-icon">&#xe67c;</i>
                    选择文件
                </button>
                <div class="file-preview" id="filePreview">
                    <c:if
                            test="${!empty tenterprise.changeMaterials && fn:length(tenterprise.changeMaterials) > 0 && tenterprise.changeMaterials != 'null'}">
                        <div class="file-item">
                            <div class="file-remove" onclick="removeUploadedFile()"><i class="layui-icon">&#x1006;</i>
                            </div>
                            <div class="file-icon">
                                <c:choose>
                                    <c:when
                                            test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileType == 'image'}">
                                        <img src="${ctx}/files/${fileInfo.serverFileName}" class="file-preview-img"
                                             onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"/>
                                        <div style="display: none; color: #999; font-size: 12px">图片加载失败</div>
                                    </c:when>
                                    <c:when
                                            test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileType == 'excel'}">
                                        <i class="fas fa-file-excel excel"></i>
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileType == 'pdf'}">
                                        <div class="file-icon pdf">
                                            <i class="fas fa-file-pdf"></i>
                                        </div>
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileType == 'word'}">
                                        <i class="fas fa-file-word word"></i>
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileType == 'text'}">
                                        <i class="fas fa-file-alt text"></i>
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == true}">
                                        <i class="fas fa-file other"></i>
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == false}">
                                        <i class="fas fa-exclamation-triangle" style="color: #ff5722"></i>
                                    </c:when>
                                    <c:otherwise>
                                        <c:choose>
                                            <c:when
                                                    test="${fn:endsWith(tenterprise.changeMaterialName, '.jpg') || fn:endsWith(tenterprise.changeMaterialName, '.png') || fn:endsWith(tenterprise.changeMaterialName, '.gif')}">
                                                <c:set var="changeMaterialsPath"
                                                       value="${tenterprise.changeMaterialName}"/>
                                                <c:if test="${fn:startsWith(changeMaterialsPath, '/files/')}">
                                                    <c:set var="changeMaterialsPath"
                                                           value="${fn:substringAfter(changeMaterialsPath, '/files/')}"/>
                                                </c:if>
                                                <img src="${ctx}/files/${changeMaterialsPath}" class="file-preview-img"
                                                     onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"/>
                                                <div style="display: none; color: #999; font-size: 12px">图片加载失败
                                                </div>
                                            </c:when>
                                            <c:when
                                                    test="${fn:endsWith(tenterprise.changeMaterialName, '.xls') || fn:endsWith(tenterprise.changeMaterialName, '.xlsx')}">
                                                <i class="fas fa-file-excel excel"></i>
                                            </c:when>
                                            <c:when test="${fn:endsWith(tenterprise.changeMaterialName, '.pdf')}">
                                                <div class="file-icon pdf">
                                                    <i class="fas fa-file-pdf"></i>
                                                </div>
                                            </c:when>
                                            <c:when
                                                    test="${fn:endsWith(tenterprise.changeMaterialName, '.doc') || fn:endsWith(tenterprise.changeMaterialName, '.docx')}">
                                                <i class="fas fa-file-word word"></i>
                                            </c:when>
                                            <c:otherwise>
                                                <i class="fas fa-file other"></i>
                                            </c:otherwise>
                                        </c:choose>
                                    </c:otherwise>
                                </c:choose>
                            </div>
                            <div class="file-name" title="${!empty fileInfo ? fileInfo.fileName : '已上传文件'}">
                                <c:choose>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileName != null}">
                                        ${fileInfo.fileName}
                                    </c:when>
                                    <c:when test="${!empty fileInfo && fileInfo.exists == false}">
                                        <span style="color: #ff5722">${fileInfo.fileName}</span>
                                    </c:when>
                                    <c:otherwise>已上传文件</c:otherwise>
                                </c:choose>
                            </div>
                            <c:if test="${!empty fileInfo && fileInfo.exists == true && fileInfo.fileSize != null}">
                                <div class="file-size" style="font-size: 10px; color: #999; margin-top: 2px">
                                    <c:choose>
                                        <c:when test="${fileInfo.fileSize > 1048576}">
                                            <fmt:formatNumber value="${fileInfo.fileSize / 1048576.0}"
                                                              maxFractionDigits="1"/>
                                            MB
                                        </c:when>
                                        <c:when test="${fileInfo.fileSize > 1024}">
                                            <fmt:formatNumber value="${fileInfo.fileSize / 1024.0}"
                                                              maxFractionDigits="1"/>
                                            KB
                                        </c:when>
                                        <c:otherwise>${fileInfo.fileSize}B</c:otherwise>
                                    </c:choose>
                                </div>
                            </c:if>
                        </div>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="formBtn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="button" id="rollback" class="layui-btn layui-btn-primary">返回</button>
        </div>
    </div>
</form>

<script>
    var ctxPath = "${ctx}";

    // 格式化文件大小
    function formatFileSize(bytes) {
        if (bytes === 0) return "0 Bytes";
        var k = 1024;
        var sizes = ["Bytes", "KB", "MB", "GB"];
        var i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
    }

    // 清除已选文件
    function clearFile() {
        layui.use(["jquery", "layer"], function () {
            var $ = layui.$;
            var layer = layui.layer;
            layer.confirm("确定要移除这个文件吗？", function (index) {
                $("#filePreview").html("");
                $("#changeMaterials").val("");
                // 清空文件输入框，确保可以重新选择相同文件
                $("#fileInput").val("");
                layer.close(index);
            });
        });
    }

    // 移除已上传的文件
    function removeUploadedFile() {
        layui.use(["jquery", "layer"], function () {
            var $ = layui.$;
            var layer = layui.layer;
            layer.confirm("确定要移除这个文件吗？", function (index) {
                $("#filePreview").html("");
                $("#changeMaterials").val("");
                // 清空文件输入框，确保可以重新选择相同文件
                $("#fileInput").val("");
                layer.close(index);
            });
        });
    }

    // 移除已上传的许可证图片文件
    function removeLicenseImgFile() {
        layui.use(['jquery', 'layer'], function () {
            var $ = layui.$;
            var layer = layui.layer;
            layer.confirm('确定要移除这个文件吗？', function (index) {
                $("#licenseImgPreview").html('');
                $("#licenseImgFile").val('');
                layer.close(index);
            });
        });
    }   // 移除已上传的营业执照图片文件
    function removeBusinessLicenseImgFile() {
        layui.use(['jquery', 'layer'], function () {
            var $ = layui.$;
            var layer = layui.layer;
            layer.confirm('确定要移除这个文件吗？', function (index) {
                $("#businessLicenseImgPreview").html('');
                $("#businessLicenseImgFile").val('');
                layer.close(index);
            });
        });
    }

    document.getElementById("rollback").addEventListener("click", function () {
        window.history.back();
    });
    layui.use(["form", "laydate", "tree", "layer", "upload"], function () {
        //初始化
        var $ = layui.$,
            layer = layui.layer,
            upload = layui.upload,
            form = layui.form;
        /* 区域树 */
        $(".area-select-container").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡
            // 关闭其他下拉框
            $(".industryshow").fadeOut();
            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".areaTreeshow").is(":hidden")) {
                $(".areaTreeshow").fadeIn();
                $("#areaArrow").css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".areaTreeshow").fadeOut();
                $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 单独为箭头添加点击事件，确保箭头点击有效
        $("#areaArrow").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 关闭其他下拉框
            $(".industryshow").fadeOut();
            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".areaTreeshow").is(":hidden")) {
                $(".areaTreeshow").fadeIn();
                $(this).css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".areaTreeshow").fadeOut();
                $(this).css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 清空按钮点击事件
        $("#areaClear").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 清空选择
            $("#t_a_id").val("");
            $("#areaDiv").html('<span style="color: #999;margin-left:10px;">请选择区域</span>');
            $(".areaTreeshow").fadeOut();
            $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(this).hide(); // 隐藏清空按钮
        });

        var url = "${ctx}/enterprise/getAreaTree";
        $.ajax({
            type: "post", //数据传输方式
            url: url, //请求路径
            dataType: "json", //预期服务器返回的数据类型 text,json
            scriptCharset: "utf-8",
            success: function (msg) {
                layui.tree({
                    elem: "#areaTree",
                    nodes: msg,
                    click: function (node) {
                        $("#t_a_id").val(node.id);
                        $("#areaDiv").html('<span style="color: #333;margin-left:10px;">' + node.name + "</span>");
                        $(".areaTreeshow").fadeOut();
                        $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
                        $("#areaClear").show(); // 显示清空按钮
                    },
                });
            },
        });

        /* 行业类别树 */
        $(".industry-select-container").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡
            // 关闭其他下拉框
            $(".areaTreeshow").fadeOut();
            $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".industryshow").is(":hidden")) {
                $(".industryshow").fadeIn();
                $("#industryArrow").css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".industryshow").fadeOut();
                $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 单独为箭头添加点击事件，确保箭头点击有效
        $("#industryArrow").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 关闭其他下拉框
            $(".areaTreeshow").fadeOut();
            $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".industryshow").is(":hidden")) {
                $(".industryshow").fadeIn();
                $(this).css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".industryshow").fadeOut();
                $(this).css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 清空按钮点击事件
        $("#industryClear").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 清空选择
            $("#t_i_id").val("");
            $("#industryDiv").html('<span style="color: #999;margin-left:10px;">请选择行业类别</span>');
            $(".industryshow").fadeOut();
            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(this).hide(); // 隐藏清空按钮
        });

        var url = "${ctx}/enterprise/getIndustryTree";
        $.ajax({
            type: "post", //数据传输方式
            url: url, //请求路径
            dataType: "json", //预期服务器返回的数据类型 text,json
            scriptCharset: "utf-8",
            success: function (msg) {
                layui.tree({
                    elem: "#industryTree",
                    nodes: msg,
                    click: function (node) {
                        if (node.children == null) {
                            $("#t_i_id").val(node.id);
                            $("#industryDiv").html('<span style="color: #333;margin-left:10px;">' + node.name + "</span>");
                            $(".industryshow").fadeOut();
                            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
                            $("#industryClear").show(); // 显示清空按钮
                        }
                    },
                });
            },
        });

        /* 生产范围树 */
        $(".procla-select-container").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡
            // 关闭其他下拉框
            $(".areaTreeshow").fadeOut();
            $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".industryshow").fadeOut();
            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".proclashow").is(":hidden")) {
                // 打开下拉框时，保存当前选择状态
                saveProclaSelection();
                $(".proclashow").fadeIn();
                $("#proclaArrow").css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".proclashow").fadeOut();
                $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 单独为箭头添加点击事件，确保箭头点击有效
        $("#proclaArrow").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 关闭其他下拉框
            $(".areaTreeshow").fadeOut();
            $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(".industryshow").fadeOut();
            $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");

            if ($(".proclashow").is(":hidden")) {
                // 打开下拉框时，保存当前选择状态
                saveProclaSelection();
                $(".proclashow").fadeIn();
                $(this).css("transform", "translateY(-50%) rotate(180deg)");
            } else {
                $(".proclashow").fadeOut();
                $(this).css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 保存当前确认的选择状态（用于取消时恢复）
        var savedProclaSelection = {
            ids: [],
            titles: [],
        };

        // 清空按钮点击事件
        $("#proclaClear").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 清空所有选择
            if (xtree1 && typeof xtree1.SetChecked === "function") {
                xtree1.SetChecked([]);
            }
            // 取消所有复选框
            $("#proclaTree input[type='checkbox']").prop("checked", false);
            form.render("checkbox"); // 重新渲染复选框
            // 更新显示
            $("#proclaId").val("");
            $("#proclaDiv").html('<span style="color: #999;margin-left:10px;">请选择生产范围</span>');
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            $(this).hide(); // 隐藏清空按钮
            // 清空保存的选择状态
            savedProclaSelection.ids = [];
            savedProclaSelection.titles = [];
        });

        // 确认按钮点击事件
        $("#proclaConfirm").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 更新显示
            updateProclaDisplay();
            // 保存当前选择状态
            saveProclaSelection();
            // 关闭下拉框
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");
        });

        // 取消按钮点击事件
        $("#proclaCancel").click(function (e) {
            e.stopPropagation();
            e.preventDefault();
            // 恢复之前保存的选择状态
            restoreProclaSelection();
            // 关闭下拉框
            $(".proclashow").fadeOut();
            $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");
        });

        // 更新生产范围显示
        var updateProclaDisplay = function () {
            if (!xtree1 || typeof xtree1.GetChecked !== "function") {
                return; // xtree1 未初始化，直接返回
            }
            var protitle = "";
            var provalue = "";
            var proclaArray = xtree1.GetChecked();
            for (var i = 0; i < proclaArray.length; i++) {
                protitle += proclaArray[i].title + ",";
                provalue += proclaArray[i].value + ",";
            }
            if (protitle.length > 0) {
                protitle = protitle.substring(0, protitle.length - 1);
            }
            if (provalue.length > 0) {
                provalue = provalue.substring(0, provalue.length - 1);
            }

            if (protitle.length > 0) {
                // 如果选择项过多，显示前几个并加上省略号
                var displayText = protitle;
                if (displayText.length > 30) {
                    var parts = protitle.split(",");
                    if (parts.length > 2) {
                        displayText = parts[0] + "," + parts[1] + "...(" + parts.length + "项)";
                    } else {
                        displayText = protitle.substring(0, 30) + "...";
                    }
                }
                $("#proclaDiv").html(
                    '<span style="color: #333;margin-left:10px;" title="' + protitle + '">' + displayText + "</span>"
                );
                $("#proclaClear").show(); // 显示清空按钮
            } else {
                $("#proclaDiv").html('<span style="color: #999;margin-left:10px;">请选择生产范围</span>');
                $("#proclaClear").hide(); // 隐藏清空按钮
            }
            $("#proclaId").val(provalue);
        };

        // 保存当前选择状态
        var saveProclaSelection = function () {
            if (!xtree1 || typeof xtree1.GetChecked !== "function") {
                return;
            }
            var checked = xtree1.GetChecked();
            savedProclaSelection.ids = [];
            savedProclaSelection.titles = [];
            for (var i = 0; i < checked.length; i++) {
                savedProclaSelection.ids.push(checked[i].value);
                savedProclaSelection.titles.push(checked[i].title);
            }
        };

        // 恢复保存的选择状态
        var restoreProclaSelection = function () {
            if (!xtree1 || typeof xtree1.SetChecked !== "function") {
                return;
            }
            // 先取消所有选择
            xtree1.SetChecked([]);
            // 恢复之前保存的选择
            if (savedProclaSelection.ids.length > 0) {
                xtree1.SetChecked(savedProclaSelection.ids);
            }
            form.render("checkbox"); // 重新渲染复选框
        };

        // 点击其他地方关闭下拉框
        $(document).click(function (e) {
            // 检查点击是否在下拉框容器内，包括树形结构
            if (!$(e.target).closest(".area-select-container").length && !$(e.target).closest(".areaTreeshow").length) {
                $(".areaTreeshow").fadeOut();
                $("#areaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
            if (
                !$(e.target).closest(".industry-select-container").length &&
                !$(e.target).closest(".industryshow").length
            ) {
                $(".industryshow").fadeOut();
                $("#industryArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
            if (!$(e.target).closest(".procla-select-container").length && !$(e.target).closest(".proclashow").length) {
                // 点击外部关闭时，恢复之前保存的选择状态
                restoreProclaSelection();
                $(".proclashow").fadeOut();
                $("#proclaArrow").css("transform", "translateY(-50%) rotate(0deg)");
            }
        });

        // 为树形结构添加点击事件处理，防止点击树形结构时关闭下拉框
        $(".areaTreeshow").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡到document
        });
        $(".industryshow").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡到document
        });
        $(".proclashow").click(function (e) {
            e.stopPropagation(); // 阻止事件冒泡到document
        });

        // 为树形结构内的所有元素添加点击事件处理
        $(document).on("click", ".areaTreeshow *", function (e) {
            e.stopPropagation(); // 阻止事件冒泡
        });
        $(document).on("click", ".industryshow *", function (e) {
            e.stopPropagation(); // 阻止事件冒泡
        });
        $(document).on("click", ".proclashow *", function (e) {
            e.stopPropagation(); // 阻止事件冒泡
        });

        var procalJson = [];
        var xtree1 = new layuiXtree({
            elem: "proclaTree", //(必填) 放置xtree的容器，样式参照 .xtree_contianer
            form: form, //(必填) layui 的 from
            data: [], //(必填) json数据
        });
        var url = "${ctx}/enterprise/getProclaTree";
        $.ajax({
            type: "post", //数据传输方式
            url: url, //请求路径
            data: {ids: $("#proclaId").val()},
            dataType: "json", //预期服务器返回的数据类型 text,json
            scriptCharset: "utf-8",
            success: function (msg) {
                xtree1 = new layuiXtree({
                    elem: "proclaTree", //(必填) 放置xtree的容器，样式参照 .xtree_contianer
                    form: form, //(必填) layui 的 from
                    data: msg, //(必填) json数据
                    ckall: true, //启用全选功能，默认值：false
                    isopen: false, //加载完毕后的展开状态，默认值：true
                });
                xtree1.render();

                // 初始显示已选择的内容
                updateProclaDisplay();
                // 保存初始选择状态
                saveProclaSelection();
            },
        });

        /* 选择时间 */
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: "#approvalTime", //指定元素
            type: "datetime",
        });
        laydate.render({
            elem: "#grantDate", //指定元素
            type: "datetime",
            format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: "#invalidDate", //指定元素
            type: "datetime",
            format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: "#grantDate2", //指定元素
            type: "datetime",
            format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: "#invalidDate2", //指定元素
            type: "datetime",
            format: 'yyyy-MM-dd'
        });

        // var changeMaterials = $('#file')[0].files.length > 0 || $('#filePreview .file-item').length > 0;
        // var licenseImg = $('#licenseImgFile')[0].files.length > 0 || $('#licenseImgPreview .file-item').length > 0;
        //
        // if (!changeMaterials || !licenseImg) {
        //     layer.msg('请上传所有必需的文件和图片！', {icon: 2});
        //     return false;
        // }

        //许可证照片
        $('#licenseImgUpload').click(function () {
            $('#licenseImgFile').click();
        });        //营业执照照片
        $('#businessLicenseImgUpload').click(function () {
            $('#businessLicenseImgFile').click();
        });


        /* 营业执照图片上传 */
        upload.render({
            elem: "#licence2",
            url: "${ctx}/enterprise/uploadFile",
            multiple: false, //是否开启多文件上传
            auto: false, //是否自动上传 如果否需要使用bindAction按钮上传
            bindAction: "#uploadLicence2",
            before: function (obj) {
                layer.load();
            },
            choose: function (obj) {
                var files = (this.files = obj.pushFile()); //将每次选择的文件追加到文件队列
                obj.preview(function (index, file, result) {
                    var div = $("#looklicence2").html(
                        '<div><img style="width: 100px;height: 100px;" src="' +
                        result +
                        '" alt="' +
                        file.name +
                        '" class="layui-upload-img"></div>'
                    );
                });
            },
            done: function (res, index, upload) {
                $("#licenceImg2").val(res.fileName);
                $("#licenceTimg2").val(res.thumbnails);
                $("#licence2").remove();
                $("#uploadLicence2").remove();

                layer.closeAll("loading");
            },
        });

        // 文件选择按钮点击事件
        $("#uploadBtn").click(function () {
            $("#fileInput").click();
        });
        // 许可证图片文件选择事件
        $('#licenseImgFile').change(function () {
            var file = this.files[0];
            if (file) {
                // 验证文件类型，只允许图片
                if (!file.type.startsWith('image/')) {
                    layer.msg('请选择图片文件！', {icon: 2});
                    $(this).val('');
                    return;
                }

                // 显示图片预览
                $("#licenseImgPreview").html('');
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("#licenseImgPreview").append('<div class="file-item">' +
                        '<div class="file-remove" onclick="removeBusinessLicenseImgFile()"><i class="layui-icon">&#x1006;</i></div>' +
                        '<div class="file-icon"><img src="' + e.target.result + '" class="file-preview-img"></div>' +
                        '<div class="file-name" title="' + file.name + '">' + file.name + '</div>' +
                        '</div>');
                };
                reader.readAsDataURL(file);
            }
        });
        // 营业执照图片文件选择事件
        $('#businessLicenseImgFile').change(function () {
            var file = this.files[0];
            if (file) {
                // 验证文件类型，只允许图片
                if (!file.type.startsWith('image/')) {
                    layer.msg('请选择图片文件！', {icon: 2});
                    $(this).val('');
                    return;
                }

                // 显示图片预览
                $("#businessLicenseImgPreview").html('');
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("#businessLicenseImgPreview").append('<div class="file-item">' +
                        '<div class="file-remove" onclick="removeBusinessLicenseImgFile()"><i class="layui-icon">&#x1006;</i></div>' +
                        '<div class="file-icon"><img src="' + e.target.result + '" class="file-preview-img"></div>' +
                        '<div class="file-name" title="' + file.name + '">' + file.name + '</div>' +
                        '</div>');
                };
                reader.readAsDataURL(file);
            }
        });
        // 文件选择事件
        $("#fileInput").change(function () {
            var file = this.files[0];
            if (file) {
                // 显示文件预览
                $("#filePreview").html("");
                var fileIcon = "other";
                var iconClass = "fas fa-file";

                if (file.type.indexOf("image") !== -1) {
                    // 如果是图片，显示图片预览
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $("#filePreview").append(
                            '<div class="file-item">' +
                            '<div class="file-remove" onclick="clearFile()"><i class="layui-icon">&#x1006;</i></div>' +
                            '<div class="file-icon"><img src="' +
                            e.target.result +
                            '" class="file-preview-img"></div>' +
                            '<div class="file-name" title="' +
                            file.name +
                            '">' +
                            file.name +
                            "</div>" +
                            "</div>"
                        );
                    };
                    reader.readAsDataURL(file);
                } else {
                    // 根据文件类型显示对应图标
                    if (
                        file.name.endsWith(".xls") ||
                        file.name.endsWith(".xlsx") ||
                        file.type.indexOf("spreadsheet") !== -1
                    ) {
                        fileIcon = "excel";
                        iconClass = "fas fa-file-excel";
                    } else if (file.name.endsWith(".pdf") || file.type.indexOf("pdf") !== -1) {
                        fileIcon = "pdf";
                        iconClass = "fas fa-file-pdf";
                    } else if (
                        file.name.endsWith(".doc") ||
                        file.name.endsWith(".docx") ||
                        file.type.indexOf("word") !== -1
                    ) {
                        fileIcon = "word";
                        iconClass = "fas fa-file-word";
                    }

                    $("#filePreview").append(
                        '<div class="file-item">' +
                        '<div class="file-remove" onclick="clearFile()"><i class="layui-icon">&#x1006;</i></div>' +
                        '<div class="file-icon ' +
                        fileIcon +
                        '"><i class="' +
                        iconClass +
                        '"></i></div>' +
                        '<div class="file-name" title="' +
                        file.name +
                        '">' +
                        file.name +
                        "</div>" +
                        "</div>"
                    );
                }
            }
        });

        // 表单验证规则
        form.verify({
            isName: function (value, item) {
                //value：表单的值、item：表单的DOM对象
                var reg = /[a-zA-Z\u4e00-\u9fa5]$/;
                if (!reg.test(value)) {
                    return "只能输入中文和字母";
                }
            },
            isPostCode: function (value, item) {
                //value：表单的值、item：表单的DOM对象
                var reg = /^[1-9][0-9]{5}$/;
                if (!reg.test(value)) {
                    return "请输入正确的邮编";
                }
            },
            isWebsite: function (value, item) {
                //value：表单的值、item：表单的DOM对象
                var reg = /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?$/;
                if (!reg.test(value)) {
                    return "请输入正确的网址";
                }
            },
        });

        /* 监听提交 */
        form.on("submit(formDemo)", function (data) {
            console.log("[表单提交] 事件触发");
            console.log("[表单提交] 完整表单数据:", data.field);

            var isEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            var email = $("#email").val();
            console.log("[表单提交] t_a_id:", $("#t_a_id").val());
            console.log("[表单提交] t_i_id:", $("#t_i_id").val());
            console.log("[表单提交] proclaId:", $("#proclaId").val());
            console.log("[表单提交] changeMaterials隐藏字段值:", $("#changeMaterials").val());

            // 检查文件输入框
            var fileInput = document.getElementById("fileInput");
            console.log("[表单提交] 文件输入框元素:", fileInput);
            if (fileInput && fileInput.files && fileInput.files.length > 0) {
                console.log("[表单提交] ✓ 文件已选择:", fileInput.files[0].name);
                console.log("[表单提交] ✓ 文件大小:", fileInput.files[0].size, "字节");
                console.log("[表单提交] ✓ 文件类型:", fileInput.files[0].type);
            } else {
                console.log("[表单提交] ✗ 警告: 没有选择新文件");
                console.log("[表单提交] ✓ 将使用隐藏字段中的旧文件路径:", $("#changeMaterials").val());
            }

            if ($("#t_a_id").val() == "") {
                alert("请选择区域");
                return false;
            } else if ($("#t_i_id").val() == "") {
                alert("请选择行业类别");
                return false;
            } else if ($("#proclaId").val() == "") {
                alert("请选择生产范围");
                return false;
            } else {
                console.log("[表单提交] ✓ 验证通过，准备提交");

                // 打印表单完整信息
                var form = $("form.layui-form");
                var formAction = form.attr("action");
                var fullUrl = window.location.origin + formAction;

                console.log("[表单提交] 表单 action:", formAction);
                console.log("[表单提交] 完整 URL:", fullUrl);
                console.log("[表单提交] 表单 method:", form.attr("method"));
                console.log("[表单提交] 表单 enctype:", form.attr("enctype"));
                console.log("[表单提交] 当前页面URL:", window.location.href);

                layer.load(1, {
                    shade: [0.1, "#fff"], //0.1透明度的白色背景
                });

                // 直接提交表单，让后端处理文件上传（与 resumptionForm.jsp 保持一致）
                return true;
            }
        });
    });
</script>
</body>

</html>